package com.somsoft.handjc.activity.query.xzzf;

import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListAdapter;

import com.somsoft.handjc.activity.R;
import com.somsoft.handjc.util.BaseActivity;
import com.somsoft.handjc.util.MyApplication;
import com.somsoft.handjc.util.XzzfDataUtil;

public class XzzfQueryActivity extends BaseActivity {
	private Button btnSubmit = null;
	private Button btnClear = null;
	private Button btnReset = null;
	private EditText dept = null;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		MyApplication.getInstance().addActivity(this);
		setContentView(R.layout.xzzf_zhonghe_query);
		setTitle("行政执法综合查询条件");
		btnSubmit = (Button)findViewById(R.id.btnSubmit);
		btnClear = (Button)findViewById(R.id.btnClear);
		btnReset = (Button)findViewById(R.id.btnReset);
		dept = (EditText)findViewById(R.id.xzzf_query_dept);
		
		btnSubmit.setOnClickListener(new OnClickListenerImpl());
		btnClear.setOnClickListener(new OnClickListenerImpl());
		btnReset.setOnClickListener(new OnClickListenerImpl());
		dept.setOnClickListener(new OnClickListenerImpl());
		dept.setOnFocusChangeListener(new OnFocusChangeListenerImpl());
	}
	
	private class OnFocusChangeListenerImpl implements OnFocusChangeListener{

		@Override
		public void onFocusChange(View view, boolean hasFocus) {
			if(hasFocus){
				final EditText xzzf_query_dept = (EditText)findViewById(R.id.xzzf_query_dept);
				ListAdapter adapter = new ArrayAdapter<String>(XzzfQueryActivity.this, R.layout.simple_list_item_2,getData()); 
				
				AlertDialog.Builder builder = new AlertDialog.Builder(XzzfQueryActivity.this);
				builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						xzzf_query_dept.setText(XzzfDataUtil.xzzfDeptNames.get(which));
					}
				});
				builder.setNegativeButton("取消",new DialogInterface.OnClickListener(){
					@Override
					public void onClick(DialogInterface dialog, int which) {
						try {
							dialog.dismiss();
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
					
				});
				AlertDialog alertDialog = builder.create();
				alertDialog.show();
				
				WindowManager.LayoutParams  lp= alertDialog.getWindow().getAttributes();
			    lp.height=900;//定义高度
			    alertDialog.getWindow().setAttributes(lp);
			}
			
		}
		
	}
	
	private class OnClickListenerImpl implements OnClickListener{

		@Override
		public void onClick(View v) {
//			Button btn = (Button)v;
			int id = v.getId();
			EditText caseName = (EditText)findViewById(R.id.caseName);
			EditText caseCode = (EditText)findViewById(R.id.caseCode);
			EditText xzzf_query_breaker = (EditText)findViewById(R.id.xzzf_query_breaker);
			final EditText xzzf_query_dept = (EditText)findViewById(R.id.xzzf_query_dept);
			
			Intent it = new Intent(XzzfQueryActivity.this,XzzfZhongHeQueryActivity.class);
			if(id == R.id.btnSubmit){
				it.putExtra("caseName", caseName.getText().toString());
				it.putExtra("caseCode", caseCode.getText().toString());
				it.putExtra("breaker", xzzf_query_breaker.getText().toString());
				it.putExtra("dept", xzzf_query_dept.getText().toString());
				startActivity(it);
			}else if(id == R.id.btnClear){
				caseName.setText("");
				caseCode.setText("");
				xzzf_query_breaker.setText("");
				xzzf_query_dept.setText("");
			}else if(id == R.id.btnReset){
				startActivity(it);
			}else if(id == R.id.xzzf_query_dept){
				ListAdapter adapter = new ArrayAdapter<String>(XzzfQueryActivity.this, R.layout.simple_list_item_2,getData()); 
				
				AlertDialog.Builder builder = new AlertDialog.Builder(XzzfQueryActivity.this);
				builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						xzzf_query_dept.setText(XzzfDataUtil.xzzfDeptNames.get(which));
					}
				});
				builder.setNegativeButton("取消",new DialogInterface.OnClickListener(){
					@Override
					public void onClick(DialogInterface dialog, int which) {
						try {
							dialog.dismiss();
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
					
				});
				AlertDialog alertDialog = builder.create();
				alertDialog.show();
				
				WindowManager.LayoutParams  lp= alertDialog.getWindow().getAttributes();
			    lp.height=900;//定义高度
			    alertDialog.getWindow().setAttributes(lp);
			}
		}
	}
	
	private List<String> getData(){ 
        List<String> data = new ArrayList<String>(); 
        if(XzzfDataUtil.xzzfDeptNames != null){
        	data = XzzfDataUtil.xzzfDeptNames;
        }
        return data; 
    }
	
}